<?php

class DocumentController extends GenesisController {

    public function manageDocumentGroupsAction() {

        $this->view->headLink()->appendStylesheet('/js/jqGrid/themes/ui.jqgrid.css');
        $this->view->headScript()->appendFile('/js/jqGrid/js/i18n/grid.locale-en.js');
        $this->view->headScript()->appendFile('/js/jqGrid/js/jquery.jqGrid.js');
        
    }

    public function showDocumentTableGridAction() {
        $this->view->headScript()->appendFile('/js/jquery/ajaxupload.js');
        $this->view->headLink()->appendStylesheet('/js/jqGrid/themes/ui.jqgrid.css');
        $this->view->headScript()->appendFile('/js/jqGrid/js/i18n/grid.locale-en.js');
        $this->view->headScript()->appendFile('/js/jqGrid/js/jquery.jqGrid.js');
        $id = $this->_request->getParam('groupid');

        $documentGroups = new Default_Model_DocumentGroups();
        $documentGroups->find($id);

        $this->view->groupId=$id;
        $this->view->groupTitle=$documentGroups->getTitle();
    }

    public function setDocumentTableJsonAction() {
       $id=$this->_request->getParam('id');
       $oper = $this->_request->getParam('oper');

       $groupId = $this->_request->getParam('groupid');
       
       $documentModel = new Default_Model_Documents();
       $primaryKey = $documentModel->getMapper()->getDbTable()->getPrimaryKeyName();

       
       if (in_array($oper,array('edit','del')))
           $documentModel->find($id);
       if ($oper == 'del' ) {
           $documentModel->deleteRowByPrimaryKey();
       } else {

       unset($_POST['oper']);

       foreach ($_POST as $key=>$value) {
           if ($key == 'id')
               continue;
           $parseKey='set'.$documentModel->columnName2Var($key);
           $documentModel->$parseKey($value);
       }
       $documentModel->setGroupId($groupId);
       $documentModel->save();
       }

    }

    public function getDocumentTableJsonAction() {
        $page = $this->_request->getParam('page'); // get the requested page
        $limit = $this->_request->getParam('rows'); // get how many rows we want to have into the grid
        $sidx = $this->_request->getParam('sidx'); // get index row - i.e. user click to sort
        $sord = $this->_request->getParam('sord'); // get the direction if(!$sidx) $sidx =1;
        $groupId = $this->_request->getParam('groupid');

       $documentModel = new Default_Model_Documents();
       $records = $documentModel->countByQuery('group_id = '.$groupId);

        $total=ceil(($records)/$limit);
        if ($page > $total)
                $page=$total;


    $rows=array();
    if ($records>0) $rows=$documentModel->fetchListToArray('group_id = '.$groupId, $sidx.' '.$sord, $limit, ($page-1)*$limit);
       $primaryKey=$documentModel->getMapper()->getDbTable()->getPrimaryKeyName();


       //TODO: currently the first row has to be the primary key.
       $resultRows=array();
       foreach ($rows as $row) {
           $data=array();
           unset($row['group_id']);
           foreach ($row as $value) {
               $data[]=$value;
           }
           $resultRows[]=array(
                'id'=>$row[$primaryKey],
                'cell' => $data
               );

       }

       $ret = array(
            'page'=>$page,
            'total'=>$total,
            'records'=>$records,
            'rows'=>$resultRows);
        $this->view->jsonMessage=json_encode($ret);



    }

    public function setDocumentGroupsTableJsonAction() {
       $id=$this->_request->getParam('id');
       $oper = $this->_request->getParam('oper');

       $documentGroupModel = new Default_Model_DocumentGroups();
       $primaryKey = $documentGroupModel->getMapper()->getDbTable()->getPrimaryKeyName();

       if (in_array($oper,array('edit','del')))
           $documentGroupModel->find($id);
       if ($oper == 'del' ) {
           $documentGroupModel->deleteRowByPrimaryKey();
       } else {

       unset($_POST['oper']);

       foreach ($_POST as $key=>$value) {
           if ($key == 'id')
               continue;
           $parseKey='set'.$documentGroupModel->columnName2Var($key);
           $documentGroupModel->$parseKey($value);
       }
       $documentGroupModel->save();
       }

    }

    public function getDocumentGroupsTableJsonAction() {
        $page = $this->_request->getParam('page'); // get the requested page
        $limit = $this->_request->getParam('rows'); // get how many rows we want to have into the grid
        $sidx = $this->_request->getParam('sidx'); // get index row - i.e. user click to sort
        $sord = $this->_request->getParam('sord'); // get the direction if(!$sidx) $sidx =1;

       $documentGroupModel = new Default_Model_DocumentGroups();
       $records = $documentGroupModel->countAllRows();

        $total=ceil(($records)/$limit);
        if ($page > $total)
                $page=$total;


    $rows=array();
    if ($records>0) $rows=$documentGroupModel->fetchListToArray(null, $sidx.' '.$sord, $limit, ($page-1)*$limit);
       $primaryKey=$documentGroupModel->getMapper()->getDbTable()->getPrimaryKeyName();


       //TODO: currently the first row has to be the primary key.
       $resultRows=array();
       foreach ($rows as $row) {
           $data=array();
           foreach ($row as $value) {
               $data[]=$value;
           }
           $resultRows[]=array(
                'id'=>$row[$primaryKey],
                'cell' => $data
               );

       }

       $ret = array(
            'page'=>$page,
            'total'=>$total,
            'records'=>$records,
            'rows'=>$resultRows);
        $this->view->jsonMessage=json_encode($ret);


        
    }

}